﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web;
using System.Web.UI;
using System.IO;
using Truextend.Training.Presenter.ViewInterfaces;
using Truextend.Training.Model.Entities;
using Truextend.Training.Presenter;

namespace Truextend.Training.Layouts.Truextend.Training.Popup
{
    public partial class FormCoursePopup : LayoutsPageBase, IFormCourseView
    {
        private string action = "";
        private FormCoursePresenter presenter;

        protected void Page_Load(object sender, EventArgs e)
        {
            action = Request.QueryString["action"];
            presenter = new FormCoursePresenter(this, action);
            if (action.Equals("update"))
            {
                long courseId = Convert.ToInt64(Request.QueryString["id"]);
                presenter.courseId = courseId;
            }
            presenter.InitView();

            if (!Page.IsPostBack)
            {
                presenter.LoadDataCourse();
            }
            
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            HttpContext context = HttpContext.Current;
            if (HttpContext.Current.Request.QueryString["IsDlg"] != null)
            {
                if (Page.IsValid)
                {
                    presenter.SaveCourse();
                    context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup('" + action + "');</script>");
                    context.Response.Flush();
                    context.Response.End();
                }
            }
        }

        public string Message
        {
            set
            {
                lblMessage.Text = value;
            }
        }

        public string Title
        {
            set
            {
                lblTitle.Text = value;
            }
        }

        public Course CourseToSave
        {
            get
            {
                Course course = new Course();
                course.Name = TxtName.Text;
                course.Category = DdCategory.SelectedValue;
                course.DirectedTo = TxtDirectedTo.Text;
                course.Prerequisites = TxtPrerequisites.Text;
                course.Contents = TxtContents.Text;

                return course;
            }

            set
            {
                TxtName.Text = value.Name;
                DdCategory.SelectedValue = value.Category;
                TxtDirectedTo.Text = value.DirectedTo;
                TxtPrerequisites.Text = value.Prerequisites;
                TxtContents.Text = value.Contents;
            }
        }
    }
}
